package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车的service实现类
 */
@Service
public class CartServiceImpl implements CartService {

    // @Autowired
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        // redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        // redisTemplate.setHashValueSerializer(stringSerializer);
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;


    /**
     * 加入购物车
     *
     * @param id       添加的商品id
     * @param num      添加的商品数量
     * @param username 添加到谁的购物车
     */
    @Override
    public void add(Long id, int num, String username) {
        if(num<=0) {
            redisTemplate.boundHashOps("Cart_"+username).delete(id);
            return;
        }

        Result<Sku> skuResult = skuFeign.findById(id);
        if (null != skuResult && skuResult.isFlag()) {
            Sku sku = skuResult.getData();
            Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());
            // 将sku转换成OrderItem
            OrderItem orderItem = sku2OrderItem(sku, spuResult.getData(), num);
            // 以hash类型存储购物车数据
            // namespace Cart_username
            // key  id
            // value  orderItem
            redisTemplate.boundHashOps("Cart_" + username).put(id.toString(), orderItem);
        }
    }

    /**
     * 查询购物车列表
     * @param username
     * @return
     */
    @Override
    public List<OrderItem> list(String username) {
        List list = redisTemplate.boundHashOps("Cart_" + username).values();
        return list;
    }

    /**
     * 将sku转换成orderItem
     *
     * @param sku
     * @param spu
     * @param num
     * @return
     */
    private OrderItem sku2OrderItem(Sku sku, Spu spu, int num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(spu.getId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num * orderItem.getPrice());
        orderItem.setPayMoney(num * orderItem.getPrice());
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num);

        // 分类id
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
